package com.itfreer.esb.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
 * 后台文件配置服务
 * @author gj
 *
 */
public class ConfigUtil {
	/**
	 * gis服务代理地址ip
	 */
	private static String arcgisServerHost="";
	
	/**
	 * 获取gis服务代理地址ip
	 * @return
	 */
	public static String getArcgisServerHost(){
		if(IsNullOrEmpty(arcgisServerHost)){
			arcgisServerHost=GetProperty("arcgisServer.host");
		}
		return arcgisServerHost;
	}
	
	/**
	 * gis服务代理地址Port
	 */
	private static String arcgisServerPort="";
	
	/**
	 * 获取gis服务代理地址Port
	 * @return
	 */
	public static String getArcgisServerPort(){
		if(IsNullOrEmpty(arcgisServerPort)){
			arcgisServerPort=GetProperty("arcgisServer.port");
		}
		return arcgisServerPort;
	}
	
	/**
	 * gis服务代理地址ip
	 */
	private static String arcgisServerTempHost="";
	
	/**
	 * 获取gis服务代理地址ip
	 * @return
	 */
	public static String getArcgisServerTempHost(){
		if(IsNullOrEmpty(arcgisServerTempHost)){
			arcgisServerTempHost=GetProperty("arcgisServerTemp.host");
		}
		return arcgisServerTempHost;
	}
	
	/**
	 * gis服务代理地址Port
	 */
	private static String arcgisServerTempPort="";
	
	/**
	 * 获取gis服务代理地址Port
	 * @return
	 */
	public static String getArcgisServerTempPort(){
		if(IsNullOrEmpty(arcgisServerTempPort)){
			arcgisServerTempPort=GetProperty("arcgisServerTemp.port");
		}
		return arcgisServerTempPort;
	}
	
	/**
	 * 功能服务代理地址ip
	 */
	private static String functionServerHost="";
	
	/**
	 * 获取功能服务代理地址ip
	 * @return
	 */
	public static String getFunctionServerHost(){
		if(IsNullOrEmpty(functionServerHost)){
			functionServerHost=GetProperty("functionServer.host");
		}
		return functionServerHost;
	}
	
	/**
	 * 功能服务代理地址Port
	 */
	private static String functionServerPort="";
	
	/**
	 * 获取功能服务代理地址Port
	 * @return
	 */
	public static String getFunctionServerPort(){
		if(IsNullOrEmpty(functionServerPort)){
			functionServerPort=GetProperty("functionServer.port");
		}
		return functionServerPort;
	}
	
	/**
	 * 读取properties文件
	 * @param proName
	 * @return
	 */
	public static String GetProperty(String proName){
		if(IsNullOrEmpty(proName)){
			return "";
		}
		try {  
		    	String bootPath = (ConfigUtil.class.getClassLoader().getResource("/").toURI()).getPath();
				String 	filePath=bootPath+"/properties/muleesb.properties";
				filePath = java.net.URLDecoder.decode(filePath,"utf-8");
				File pfile=new File(filePath);
				Properties propertiesFile=new Properties();
				FileInputStream inputStream=new FileInputStream(pfile);
				InputStreamReader reader=new InputStreamReader(inputStream,"utf-8");
				propertiesFile.load(reader);  
	            String proValue=propertiesFile.getProperty(proName);
	            if(proValue!=null){
	            	proValue=proValue.replace("\n", "");
	            	proValue=proValue.replace("\r", "");
	            	proValue=proValue.trim();
	            }
	            inputStream.close();  
	            return proValue;
	     } catch (Exception e1) {  
	    	 	String err = "读取propertyies 配置文件报错："+e1.getMessage();
	            System.out.println(err);  
	            return "";
	     }  
	}
	
	/**
	 * 判断字符串是空的
	 * @param str
	 * @return
	 */
	public static boolean IsNullOrEmpty(String str) {
		if(str==null) return true;
		if("".equals(str.trim())) return true;
		
		return false;
	}
}
